<?php
    class WZ_Controller_Helper_Feeder extends Zend_Controller_Action_Helper_Abstract
    {
        /**
         * @var Zend_Feed_Writer_Feed
         */
        protected $_feed;
        
        /**
         * @var string
         */
        protected $_type;
        
        /**
         * @var array
         */
        protected $_options;
        
        /**
         * @var Zend_Log
         */
        protected $_log;
        
        public function __construct($type='atom')
        {
            $this->_type    = $type;
        }
        
        public function direct()
        {
            return $this->feeder();
        }
        
        public function feeder()
        {
            return $this;
        }
        
        public function init()
        {
            $this->_feed = new Zend_Feed_Writer_Feed();
            $this->_feed->setDateModified(time());     
            
            //récupération du plugin frontcontroller
            $front = Zend_Controller_Front::getInstance();
            $bootstrap = $front->getParam('bootstrap');
            if(($logh = $bootstrap->getResource('log')) instanceof Zend_Log) {
                $this->_log = $logh;
            }
            
            //récupération des options de config
            $this->_options = $bootstrap->getOptions();
        }
        
        public function export($type = '')
        {
            $type || $type = $this->_type;
            echo $this->_feed->export($type);
        }
        
        /**
         * Accesseur de l'objet _feed
         * 
         * @return Zend_Feed_Writer_Feed
         */
        public function getFeed()
        {
           return $this->_feed;
        }
        
        /**
         * Accesseur du type
         * @return string
         */
        public function getType()
        {
            return $this->_type;
        }
        
        /**
         * @return Zend_Feed_Writer_Entry
         */
        public function createEntry()
        {
            return $this->_feed->createEntry();
        }
        
        public function addEntry(Zend_Feed_Writer_Entry $entry)
        {
            return $this->_feed->addEntry($entry);
        }
        
        /**
         * Méthode de récupération des infos sur un media (à partir de son url)
         * 
         * @param  string $mediaUrl
         * @return tableau des infos de curl_getinfo
         * @see    curl_getinfo
         */
        protected function _getInfosOnMediaUrl($mediaUrl)
        {
            try {
                $ch = curl_init($mediaUrl);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);       
                curl_exec($ch);
                $info = curl_getinfo($ch);
                return $info;
                
            } catch(Exception $e) {
                $this->_log instanceof Zend_Log && $this->_log->log("unable to get media:'$mediaUrl', ".$e, Zend_Log::WARN);
            }
        }
    }